# encoding: utf-8
class TclassesController < ApplicationController
  require 'yaml'
  # GET /tclasses
  # GET /tclasses.json
  def index
    params.each do |p|
      p.each do |pp|
        pp.strip
      end
    end
    q =""
    @f_n=""
    if !params[:t].nil? && !params[:t].empty?
      q<<" AND title like :t"
      @f_n<<" 名称包含:"<<params[:t].to_s
      params[:t] = ("%"<<params[:t]<<"%")
    end
    if !params[:d].nil? && !params[:d].empty?
      q<<" AND description like :d"
      @f_n<<" 描述包含:"<<params[:d].to_s
      params[:d] = ("%"<<params[:d]<<"%")
    end
    if !params[:s].nil? && !params[:s].empty?
      begin
        @f_n<<" 上课时间晚于:"<<params[:s].to_s
        params[:s] = params[:s].to_datetime
        q<<" AND ctime >= :s"
      rescue
      end
    end
    if !params[:e].nil? && !params[:e].empty?
      begin
        @f_n<<" 上课时间早于:"<<params[:e].to_s
        params[:e] = params[:e].to_datetime
        q<<" AND ctime <= :e"
      rescue
      end
    end
    if !q.empty?
      q= q.sub(" AND", "")
      @tclasses = Tclass.where(q, params)
    else
      @tclasses = Tclass.all
    end
    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @tclasses }
    end
  end

  # GET /tclasses/1
  # GET /tclasses/1.json
  def show
    @tclass = Tclass.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @tclass }
    end
  end

# GET /tclasses/new
# GET /tclasses/new.json
  def new
    @tclass = Tclass.new
    @groups = YAML.load(Mconfig.find_by_cname("groups").cvalue)
    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @tclass }
    end
  end

# GET /tclasses/1/edit
  def edit
    #@groups = YAML.load(Mconfig.find_by_cname("groups").cvalue)
    @tclass = Tclass.find(params[:id])
  end

# POST /tclasses
# POST /tclasses.json
  def create
    @tclass = Tclass.new(params[:tclass])
    @groups = YAML.load(Mconfig.find_by_cname("groups").cvalue)
    respond_to do |format|
      if @tclass.save
        begin
          Tclass.transaction do
            sgroups = []
            @groups.each do |g|
              if (params[g].to_s =="on")
                sgroups<<g
              end
            end
            if sgroups.size >0
              @tclass.scope = sgroups.to_s
              students = Student.order("studentId ASC").find_all_by_sgroup(sgroups)
              students.each do |s|
                a = Attendance.new
                a.student = s
                a.tclass = @tclass
                a.state = 0
                a.save
              end
              @tclass.save!
            end
          end
          format.html { redirect_to @tclass, notice: '本次课程信息创建成功.' }
          format.json { render json: @tclass, status: :created, location: @tclass }
        rescue =>e
          logger.error "创建课程出错:"<<e.to_s
        end

      else
        format.html { render action: "new" }
        format.json { render json: @tclass.errors, status: :unprocessable_entity }
      end
    end
    #render :text => params
  end

# PUT /tclasses/1
# PUT /tclasses/1.json
  def update
    @tclass = Tclass.find(params[:id])

    respond_to do |format|
      if @tclass.update_attributes(params[:tclass])
        format.html { redirect_to @tclass, notice: 'Tclass was successfully updated.' }
        format.json { head :ok }
      else
        format.html { render action: "edit" }
        format.json { render json: @tclass.errors, status: :unprocessable_entity }
      end
    end
  end

# DELETE /tclasses/1
# DELETE /tclasses/1.json
  def destroy

    @tclass = Tclass.find(params[:id])
    ActiveRecord::Base.connection().delete_sql("delete from attendances where tclass_id = "<< @tclass.id.to_s)
    @tclass.destroy
    respond_to do |format|
      format.html { redirect_to tclasses_url }
      format.json { head :ok }
    end
  end

end
